package com.ruoyi.security.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.security.domain.AfCheckLog;
import com.ruoyi.security.domain.AfElevator;
import com.ruoyi.security.domain.vo.DeviceInfoVo;
import com.ruoyi.security.mapper.AfCheckLogMapper;
import com.ruoyi.security.mapper.DeviceInfoMapper;
import com.ruoyi.security.service.IAfCheckLogService;
import com.ruoyi.security.service.IAfElevatorService;
import com.ruoyi.system.api.LinkFileServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 设备年检记录Service业务层处理
 *
 * @author cjie
 * @date 2022-06-20
 */
@Service
public class AfCheckLogServiceImpl extends LinkFileServiceImpl implements IAfCheckLogService {
    @Autowired
    private AfCheckLogMapper afCheckLogMapper;

    @Autowired
    private DeviceInfoMapper deviceInfoMapper;

    @Autowired
    private IAfElevatorService afElevatorService;

    /**
     * 查询设备年检记录
     *
     * @param id 设备年检记录ID
     * @return 设备年检记录
     */
    @Override
    public AfCheckLog selectAfCheckLogById(String id) {
        AfCheckLog result = afCheckLogMapper.selectAfCheckLogById(id);
        result.setFileList(getFileList(id.toString()));
        return result;
    }

    /**
     * 查询设备年检记录列表
     *
     * @param afCheckLog 设备年检记录
     * @return 设备年检记录
     */
    @Override
    public List<AfCheckLog> selectAfCheckLogList(AfCheckLog afCheckLog) {
        return afCheckLogMapper.selectAfCheckLogList(afCheckLog);
    }

    /**
     * 新增设备年检记录
     *
     * @param afCheckLog 设备年检记录
     * @return 结果
     */
    @Override
    public int insertAfCheckLog(AfCheckLog afCheckLog) {
        int result = 0;
        afCheckLog.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
        afCheckLog.setCreateTime(DateUtils.getNowDate());
        afCheckLog.setCreateBy(SecurityUtils.getUsername());
        if(StringUtils.isBlank(afCheckLog.getChecker())){
            afCheckLog.setChecker(afCheckLog.getCreatedBy());
        }
        // 更新电梯年检状态，年检日期，下次年检日期
        if(afCheckLog.getModuleType().equals("电梯")){
            AfElevator afElevator = new AfElevator();
            afElevator.setId(afCheckLog.getDeviceId());
            afElevator.setCheckNextDate(afCheckLog.getNextCheckDate());
            afElevator.setDeviceCheckDate(afCheckLog.getCheckDate());
            afElevator.setExpire(0);
            afElevatorService.updateAfElevator(afElevator);
            if(StringUtils.isBlank(afCheckLog.getDeviceName()) || StringUtils.isBlank(afCheckLog.getDwCode())){
                afElevator = afElevatorService.selectAfElevatorById(afCheckLog.getDeviceId());
                afCheckLog.setDeviceName(afElevator.getElevatorName());
                afCheckLog.setDwCode(afElevator.getDwCode());
            }
            result = afCheckLogMapper.insertAfCheckLog(afCheckLog);
        }
        //todo 测试消防设备
        if(afCheckLog.getModuleType().equals("消防")){
//            AfElevator afElevator = new AfElevator();
//            afElevator.setId(afCheckLog.getDeviceId());
//            afElevator.setCheckNextDate(afCheckLog.getNextCheckDate());
//            afElevator.setDeviceCheckDate(afCheckLog.getCheckDate());
//            afElevator.setExpire(0);
//            afElevatorService.updateAfElevator(afElevator);
        }

        linkFile(afCheckLog.getFileIdList(), afCheckLog.getId());
        return result;
    }

    /**
     * 修改设备年检记录
     *
     * @param afCheckLog 设备年检记录
     * @return 结果
     */
    @Override
    public int updateAfCheckLog(AfCheckLog afCheckLog) {
        afCheckLog.setUpdatedTime(DateUtils.getNowDate());
        afCheckLog.setUpdatedBy(SecurityUtils.getUsername());
        if(afCheckLog.getFileIdList() != null){
            linkFile(afCheckLog.getFileIdList(), afCheckLog.getId());
        }
        return afCheckLogMapper.updateAfCheckLog(afCheckLog);
    }

    /**
     * 批量删除设备年检记录
     *
     * @param ids 需要删除的设备年检记录ID
     * @return 结果
     */
    @Override
    public int deleteAfCheckLogByIds(String[] ids) {
        deleteLinkFile(ids);
        return afCheckLogMapper.deleteAfCheckLogByIds(ids);
    }

    /**
     * 删除设备年检记录信息
     *
     * @param id 设备年检记录ID
     * @return 结果
     */
    @Override
    public int deleteAfCheckLogById(String id) {
        return afCheckLogMapper.deleteAfCheckLogById(id);
    }

    @Override
    public List<DeviceInfoVo> selectDeviceList(String moduleType) {
        //统一响应列表
        List<DeviceInfoVo> results = null;
        if(StringUtils.isEmpty(moduleType)){
            results = deviceInfoMapper.selectElevatorDevice(new AfElevator());
        }else {
            if(moduleType.equals("电梯")){
                results = deviceInfoMapper.selectElevatorDevice(new AfElevator());
            }
        }
        return results;
    }
}
